/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2020-1-1
 */

module.exports = {
	showError(msg, callback) {
		wx.showModal({
			title: '报错提示',
			content: msg,
			showCancel: false,
			success(res) {
				callback && callback();
			}
		});
	},
	// 保留当前页面，跳转到应用内的某个页面，使用wx.navigateBack可以返回到原页面
	navigateTo: function(url) {
		wx.navigateTo({
			url: url
		})
	},
	// 关闭当前页面，返回上一页面或多级页面。可通过 getCurrentPages()) 获取当前的页面栈，决定需要返回几层。
	navigateBack: function(delta) {
		wx.navigateBack({
			delta: delta
		})
	},

	// 通过路由获取当前访问路径url
	getRouteUrl: function() {
		var pages = getCurrentPages();
		var currentPage = pages[pages.length - 1];
		var typeid = currentPage.options.typeid || 0;
		var urlData = {
			path: currentPage.route,
			typeid: typeid,
		};
		return urlData;
	},
	// 通过路由获取当前访问的完整路径url
	getCurrentPages: function() {
		var pages = getCurrentPages(); //获取加载的页面
		var currentPage = pages[pages.length - 1]; //获取当前页面的对象
		var url = currentPage.route; //当前页面url
		var options = currentPage.options; //获取url中所带的参数
		//拼接url的参数
		var currentPage = '/' + url + '?';
		for (var key in options) {
			var value = options[key]
			currentPage += key + '=' + value + '&';
		}
		currentPage = currentPage.substring(0, currentPage.length - 1);
		return currentPage;
	},
	// 复制订单号
	Copy(e) {
		wx.setClipboardData({
			data: e.currentTarget.dataset.text,
			success: function(res) {
				wx.getClipboardData({
					success: function(res) {
						wx.showToast({
							title: '复制成功',
						})
					},
				})
			}
		})
	},
	// 底部菜单选中状态切换
	setTabBar: function(that, tabbar) {
		let selected = -1;
		let blist = tabbar.list;
		let urlData = this.getRouteUrl();
		let currentPath = urlData.path;
		let typeid = urlData.typeid;
		for (let i in blist) {
			if (blist[i]) {
				let pagePath = blist[i].pagePath + `?`;
				pagePath = '/' + pagePath.replace(/(^\/*)/g, "");
				if (pagePath.indexOf(`/pages/archives/custom/list?`) > -1) {
					if (pagePath.indexOf(`?typeid=${typeid}?`) > -1 || pagePath.indexOf(`?typeid=${typeid}&`) >
						-1) {
						selected = parseInt(i);
						break;
					}
				} else if (pagePath.indexOf(`/pages/archives/custom/view?`) > -1) {
					if (pagePath.indexOf(`?typeid=${typeid}?`) > -1 || pagePath.indexOf(`?typeid=${typeid}&`) >
						-1) {
						selected = parseInt(i);
						break;
					}
				} else if (pagePath.indexOf(`/${currentPath}?`) > -1) {
					selected = parseInt(i);
					break;
				}
			}
		}
		tabbar.selected = selected;
		that.setData({
			tabbar: tabbar
		});
		try {
			wx.setStorageSync('tabbar', tabbar);
		} catch (e) {}
	},
	switchTab: function(e, jumpType) {
		let url = '';
		jumpType = jumpType || 'redirectTo';
		if (typeof e === 'object') {
			let data = e.currentTarget.dataset;
			url = data.path;
		} else {
			url = e;
		}
		let firstchat = url.substr(0, 1);
		if (firstchat != '/') url = '/' + url;

		if ('/pages/index/index' == url) {
			wx.reLaunch({
				url
			});
		} else {
			if ('navigateTo' == jumpType) {
				wx.navigateTo({
					url
				});
			} else if ('redirectTo' == jumpType) {
				wx.redirectTo({
					url
				});
			} else if ('reLaunch' == jumpType) {
				wx.reLaunch({
					url
				});
			}
		}
	},
	/**
	 * 获取URL参数的某个值
	 * @param {string} variable 
	 * @param {string} query 
	 */
	getQueryVariable: function(variable, query) {
		if (-1 < query.indexOf('?')) {
			var arr = query.split("?");
			query = arr[1];
		}
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return false;
	},
	//跳转到指定页
	jumpToUrl: function(e) {
		let url = e.currentTarget.dataset.url;
		if (!url || '' == url || undefined == url) {
			wx.showToast({
				icon: 'none',
				title: '开源版不支持',
			})
		}

		let type = e.currentTarget.dataset.type;
		var tabList=["/pages/classify/classify", "/pages/flow/index","/pages/user/index"]
		if ('' == type || undefined == type) {
			type = 'navigateTo';
		}
		if ( tabList.indexOf(url) >= 0) {
			wx.switchTab({
				url: url
			});
		}  
		if ('navigateTo' == type) {
			wx.navigateTo({
				url: url,
				// animationType:'pop-in',
			});
		} else if ('redirectTo' == type) {
			wx.redirectTo({
				url: url
			});
		} else if ('switchTab' == type) {
			wx.switchTab({
				url: url
			});
		} else if ('makePhoneCall' == type) {
			let phone = e.currentTarget.dataset.phone;
			if (phone == '') {
				wx.showToast({
					title: '亲，暂无门店电话哦~',
					icon: 'none',
				})
			} else {
				wx.makePhoneCall({
					phoneNumber: phone
				});
			}
		}
	},
}
